/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OpenWith.java
 *
 * Created on Oct 4, 2009, 8:23:48 PM
 */

package net.sf.bluex.boundary;

import java.io.File;
import java.util.Vector;
import javax.swing.JOptionPane;
import net.sf.bluex.components.BlueXFile;
import net.sf.bluex.controller.UsefulMethods;
import net.sf.bluex.plugin.ModuleDetail;
import net.sf.bluex.plugin.Plugin;
import net.sf.bluex.plugin.PluginMetaData;

/**
 *
 * @author Blue
 */
public class OpenWith extends javax.swing.JDialog {
    private BlueXFile file;

    /** Creates new form OpenWith */
    private OpenWith(java.awt.Frame parent, BlueXFile file) {
        super(parent, false);
        this.file = file;
        initComponents();
        setTitle("Open "+file.getFile().getName()+" with:");
        initPlugins();
        setLocationRelativeTo(parent);
    }

    public static void showOpenWith(java.awt.Frame parent, BlueXFile file) {
        new OpenWith(parent, file).setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Choose the plugin from which you want to open it:");

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Request OS to Run this File");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Use Selected Plugin");

        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jButton1.setText("Open");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton1)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(234, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        // TODO add your handling code here:
        if(evt.getValueIsAdjusting()){
            jRadioButton2.setSelected(true);
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if(jRadioButton1.isSelected())
            UsefulMethods.requestOSToRun(file.getFile());
        else{
            Object obj=jList1.getSelectedValue();
            if(obj!=null){
                PluginMetaData pmd=UsefulMethods.getPluginByName((String)obj);
                try{
                    Plugin plugin=UsefulMethods.getSelectedPlugin(pmd);
                    plugin.openFileAssociationPlugin(new File[]{file.getFile()});
                }catch(Exception e){
                    JOptionPane.showMessageDialog(this, "Cannot open the plugin.\n" +
                            "Plugin files encountered some problem.", "Error opening plugin", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void initPlugins() {
        Vector<String> plugins=new Vector<String>();
        for(PluginMetaData pmd : UsefulMethods.getAllInstalledPlugins(ModuleDetail.FILE_ASSOCIATION_PLUGIN)){
            plugins.add(pmd.getName());
        }
        jList1.setListData(plugins);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
